package org.jeecg.modules.waybill.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.balance.entity.WlhyBalanceIncomeExpance;
import org.jeecg.modules.balance.service.IWlhyBalanceIncomeExpanceService;
import org.jeecg.modules.system.api.SystemApi;
import org.jeecg.modules.waybill.entity.WlhyFreightSettlement;
import org.jeecg.modules.waybill.mapper.WlhyFreightSettlementMapper;
import org.jeecg.modules.waybill.service.IWlhyFreightSettlementService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Date;

import static org.jeecg.common.constant.WlhyBizConstant.*;

/**
 * @Description: 运费结算
 * @Author: jeecg-boot
 * @Date: 2024-04-17
 * @Version: V1.0
 */
@Service
public class WlhyFreightSettlementServiceImpl extends ServiceImpl<WlhyFreightSettlementMapper, WlhyFreightSettlement> implements IWlhyFreightSettlementService {

    @Resource
    SystemApi systemApi;


    @Resource
    IWlhyBalanceIncomeExpanceService wlhyBalanceIncomeExpanceService;
    /**
     * 结算
     *
     * @param id
     * @param settleFreightMode
     * @param settleFreightVoucher
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settle(String id, String settleFreightMode, String settleFreightVoucher) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        WlhyFreightSettlement settlement = getById(id);
        if (null == settlement) {
            throw new RuntimeException("数据不存在");
        }
        if (!settlement.getOrderUserId().equals(loginUser.getId())) {
            throw new RuntimeException("非法操作");
        }

        if (SETTLE_FREIGHT_MODE1.equals(settleFreightMode)) {
            // 我的钱包 -- // TODO 未注意事务性
            systemApi.updateBalance(loginUser.getId(), false, settlement.getDriverFreightFee());
            WlhyBalanceIncomeExpance loginUserIe = new WlhyBalanceIncomeExpance()
                    .setUserId(loginUser.getId())
                    .setBalance(settlement.getDriverFreightFee())
                    .setIeType(IE_TYPE2)
                    .setIeBizType(IE_BIZ_TYPE2)
                    .setSubject("支付司机运费")
                    .setBody(String.format("支付司机运费%s元",settlement.getDriverFreightFee()))
                    .setExtParam(settlement.getWaybillNo())
                    ;
            wlhyBalanceIncomeExpanceService.save(loginUserIe);
            // 它的钱包 ++
            if (StringUtils.isBlank(settlement.getDriverUserId())) {
                throw new RuntimeException("当前司机不存在");
            }
            systemApi.updateBalance(settlement.getDriverUserId(), true, settlement.getDriverFreightFee());
            WlhyBalanceIncomeExpance driverUserIe = new WlhyBalanceIncomeExpance()
                    .setUserId(settlement.getDriverUserId())
                    .setBalance(settlement.getDriverFreightFee())
                    .setIeType(IE_TYPE1)
                    .setIeBizType(IE_BIZ_TYPE3)
                    .setSubject("获取运费")
                    .setBody(String.format("获取运费%s元",settlement.getDriverFreightFee()))
                    .setExtParam(settlement.getWaybillNo())
                    ;
            wlhyBalanceIncomeExpanceService.save(driverUserIe);
            update(new LambdaUpdateWrapper<WlhyFreightSettlement>()
                    .eq(WlhyFreightSettlement::getId, id)
                    .set(WlhyFreightSettlement::getSettleFreightStatus, SETTLE_FREIGHT_STATUS2)
                    .set(WlhyFreightSettlement::getSettleFreightTime, new Date())

            );
        } else if (SETTLE_FREIGHT_MODE2.equals(settleFreightMode)) {
            if (StringUtils.isBlank(settleFreightVoucher)) {
                throw new RuntimeException("请填写银行卡相关结算凭证");
            }
            update(new LambdaUpdateWrapper<WlhyFreightSettlement>()
                    .eq(WlhyFreightSettlement::getId, id)
                    .set(WlhyFreightSettlement::getSettleFreightMode, settleFreightMode)
                    .set(WlhyFreightSettlement::getSettleFreightVoucher, settleFreightVoucher)
                    .set(WlhyFreightSettlement::getSettleFreightStatus, SETTLE_FREIGHT_STATUS2)
                    .set(WlhyFreightSettlement::getSettleFreightTime, new Date())
            );
        } else {
            throw new RuntimeException("运费结算方式不存在");
        }


    }
}
